import extnames from '../dependencies/extnames.mjs';
import dirs from '../dependencies/dirs.mjs';
import path from 'path';
import fs from 'fs';
import CopyPlugin from 'copy-webpack-plugin';


export default function (args, config) {
    const dir = path.join(dirs.run_project, 'src', 'images');

    if (!fs.existsSync(dir)) { return; }// 配置目录不存在时，不需要配置。

    if (fs.readdirSync(dir).length === 0) { return; }// 配置目录为空时，不需要配置。

    if (!config.plugins) { config.plugins = []; }

    config.plugins.push(
        new CopyPlugin({
            patterns: [
                {
                    from: dir,
                    to: path.join('images', '[path]', '[name]' + '[ext]'),
                    filter: (filePath) => {
                        const extname = path.extname(filePath).toLowerCase();
                        return extnames.images.includes(extname);
                    },
                },
            ],
            options: {
                concurrency: 33,
            },
        })
    );
};
